home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Oh!X 2001 Spring
/
Oh!X 2001 Spring Special CD-ROM (Japan).7z
/
Oh!X 2001 Spring Special CD-ROM (Japan) (Track 1).bin
/
PUZZLE
/
Puz02
/
keiro_b.c
< prev
next >
Wrap
C/C++ Source or Header
|
2000-02-20
|
2KB
|
89 lines
/*
* keiro_b.c : 幅優先探索の例題
*
*/
/*
1 3 5
B------D------F
/│ │
0 A │ │
\│ │
C------E------G
2 4 6
経路図
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define NODE 7
#define SIZE 64
/* 隣接リスト */
const char adjacent[NODE][4] = {
1, 2, -1, -1, /* A */
0, 2, 3, -1, /* B */
0, 1, 4, -1, /* C */
1, 4, 5, -1, /* D */
2, 3, 6, -1, /* E */
3, -1, -1, -1, /* F */
4, -1, -1, -1, /* G */
};
/* Queue */
char path[SIZE][NODE];
char length[SIZE];
/* 経路の表示 */
void print_path( int n, int goal )
{
int i;
int len = length[n];
for( i = 0; i <= len; i++ ){
printf("%c ", path[n][i] + 'A' );
}
printf("%c\n", goal + 'A' );
}
/* 幅優先探索 */
void search( int start, int goal )
{
int wc = 1, rc = 0;
/* 初期化 */
path[0][0] = start; /* スタート地点をセット */
length[0] = 0;
while( rc < wc ){
int len = length[rc];
int node = path[rc][len];
int i, n;
for( i = 0; (n = adjacent[node][i]) != -1; i++ ){
if( memchr( path[rc], n, len + 1 ) == NULL ){
if( n == goal ){
/* ゴールに到達 */
print_path( rc, goal );
} else {
/* 経路を進める */
memcpy( path[wc], path[rc], len + 1 );
path[wc][len + 1] = n;
length[wc] = len + 1;
wc++;
}
}
}
rc++;
}
printf("状態数 %d 個\n", wc );
}
int main()
{
search( 0, 6 );
return 0;
}
/* end of file */